#!/bin/sh
### BEGIN INIT INFO
# Provides:          zrlog
# Required-Start:    $local_fs $network $named $time $syslog
# Required-Stop:     $local_fs $network $named $time $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Description:       ZrLog is a blog/CMS program developed in Java
### END INIT INFO
# shellcheck disable=SC2188
if [ "$(whoami &2>/dev/null)" != "root" ] && [ "$(id -un &2>/dev/null)" != "root" ]
      then
      echo "You must be root to run this script!"
      exit 1
fi

PROCESS_NAME=zrlog
SERVICE_USER=zrlog           # 运行服务的用户
PIDFILE=/var/run/${PROCESS_NAME}/${PROCESS_NAME}.pid
PORTFILE=/var/run/${PROCESS_NAME}/http-${PROCESS_NAME}.port
STOP_SCHEDULE="${STOP_SCHEDULE:-QUIT/1/TERM/5/KILL/1}"
ENABLE_DEV_MODE='false'
UPGRADE_VERSION='release'
configFile="/etc/${PROCESS_NAME}/${PROCESS_NAME}.properties"

prop() {
    sed -n -e "s/^$1=\(.*\)/\1/p" "$configFile"
}

VM_ARG=$(prop "vmArgs")

dirs="
/etc/${PROCESS_NAME}
/var/cache/${PROCESS_NAME}
/var/run/${PROCESS_NAME}
/var/log/${PROCESS_NAME}
/var/${PROCESS_NAME}
/var/lib/${PROCESS_NAME}
"

for dir in $dirs; do
    mkdir -p "$dir"
    chown -hR ${SERVICE_USER}:${SERVICE_USER} "$dir"
done

backup() {
    # Create the /var/backups directory if it doesn't exist.
    if [ ! -d /var/backups ]; then
        mkdir /var/backups
    fi
    if [ -d /etc/${PROCESS_NAME} ]; then
      tar -czf /var/backups/${PROCESS_NAME}_v1.tar.gz -C  /etc ${PROCESS_NAME}
    fi
}


start() {
  # shellcheck disable=SC2046
  if [ -f ${PIDFILE} ] && [ -d /proc/$(cat ${PIDFILE})/ ];
  then
        echo ${PROCESS_NAME}' already running';
        exit 0;
  fi
  echo 'Starting service…'
  sudo -u $SERVICE_USER -- env \
      PATH="$PATH" \
      ZRLOG_PID_FILE=${PIDFILE} \
      ZRLOG_HOME="/var/lib/${PROCESS_NAME}" \
      ZRLOG_HTTP_PORT_FILE=${PORTFILE} \
      ZRLOG_DISABLE_BIND_PORT_ERROR_RETRY_ABLE="true" \
      DEV_MODE=${ENABLE_DEV_MODE} \
      SYSTEM_SERVICE_MODE="true" \
      bash -c "/usr/bin/${PROCESS_NAME} ${VM_ARG}  </dev/null >/dev/null 2>&1 & disown"
  echo 'Service started'
}

reload() {
    if [ ! -f ${PIDFILE} ]; then
      echo ${PROCESS_NAME}' not running'
      return 2
    fi
    # shellcheck disable=SC2046
    kill -HUP $(cat ${PIDFILE})
    echo ${PROCESS_NAME}' config reloaded';
    exit 0;
}

dump() {
    if [ ! -f ${PIDFILE} ]; then
      echo ${PROCESS_NAME}' not running'
      return 1
    fi
    # shellcheck disable=SC2046
    kill -USR1 $(cat ${PIDFILE})
    echo ${PROCESS_NAME}' dump success';
    exit 0;
}

dev() {
    if [ ! -f ${PIDFILE} ]; then
      echo ${PROCESS_NAME}' not running'
      return 2
    fi
    # shellcheck disable=SC2046
    kill -USR2 $(cat ${PIDFILE})
    echo ${PROCESS_NAME}' dev enabled';
    exit 0;
}

stop() {
  # shellcheck disable=SC2046
  if [ ! -f ${PIDFILE} ]; then
    echo 'Service not running'
    return 1
  fi
  # shellcheck disable=SC2046
  if [ ! -d /proc/$(cat ${PIDFILE})/ ]; then
    echo 'Process not running'
    return 2
  fi
  echo 'Stopping service…'
  start-stop-daemon --stop --quiet --retry="${STOP_SCHEDULE}" --pidfile ${PIDFILE} --user ${SERVICE_USER}
  rm -f ${PIDFILE}
  echo 'Service stopped'
}

upgrade() {
  architecture=$(dpkg --print-architecture)
  fileName=${PROCESS_NAME}-Linux-${architecture}.deb
  fullFileName=/tmp/${fileName}
  test -f "${fullFileName}" && rm "${fullFileName}"
  sudo -u $SERVICE_USER \
    bash -c "wget -q --show-progress https://dl.zrlog.com/${UPGRADE_VERSION}/${fileName} -O ${fullFileName}"
  dpkg -i "${fullFileName}"
  exit 0
}

case "$1" in
  reload)
    reload
    ;;
  dump)
    dump
    ;;
  dev)
    dev
    ;;
  start)
    start
    ;;
  start-dev)
    ENABLE_DEV_MODE='true'
    start
    ;;
  stop)
    stop
    ;;
  restart)
    stop
    start
    ;;
  force-start)
    stop
    start
    ;;
  upgrade)
    backup
    upgrade
    ;;
  upgrade-preview)
    backup
    UPGRADE_VERSION="preview"
    upgrade
    ;;
  *)
    echo "Usage: $0 {start|force-start|start-dev|stop|reload|restart|upgrade|upgrade-preview|dump|dev}"
esac
